/**
 * 自定义校验函数
 * @param fn 值验证函数
 * @param message 错误提示
 */
export function customValidator(fn: (value: any) => boolean | Promise<boolean>, message: string) {
  return (rule: any, value: any) => {
    return new Promise<void>((resolve, reject) => {
      const ret = fn(value);
      if (typeof ret === "boolean") {
        if (ret) {
          resolve();
        } else {
          reject(message);
        }
      } else {
        ret.then(ok => (ok ? resolve() : reject(message))).catch(e => reject(e?.message ?? e?.toString()));
      }
    });
  };
}

/**
 * 校验文件夹是否存在
 */
export const validateDirExists = (_: any, value: string) => {
  return new Promise<void>((resolve, reject) => {
    native.fs
      .exists(value)
      .then(exists => (exists ? resolve() : reject("文件夹不存在")))
      .catch(e => reject(e?.message ?? e?.toString()));
  });
};

/**
 * 校验文件是否存在
 */
export const validateFileExists = (_: any, value: string) => {
  return new Promise<void>((resolve, reject) => {
    native.fs
      .exists(value)
      .then(exists => (exists ? resolve() : reject("文件不存在")))
      .catch(e => reject(e?.message ?? e?.toString()));
  });
};
